package com.shangdiao.sd_server.c_client.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shangdiao.sd_server.constant.Constant;
import com.shangdiao.sd_server.constant.ErrorCode;
import com.shangdiao.sd_server.generator.pojo.SdCompany;
import com.shangdiao.sd_server.generator.pojo.SdCompanyBase;
import com.shangdiao.sd_server.generator.pojo.SdCompanyPhoto;
import com.shangdiao.sd_server.generator.pojo.SdCompanyPhotoExample;
import com.shangdiao.sd_server.generator.pojo.SdFeedback;
import com.shangdiao.sd_server.generator.pojo.SdInternship;
import com.shangdiao.sd_server.generator.pojo.SdInternshipExample;
import com.shangdiao.sd_server.generator.pojo.SdPInternshipExample;
import com.shangdiao.sd_server.generator.pojo.SdParttime;
import com.shangdiao.sd_server.pojo.ResponseMsg;
import com.shangdiao.sd_server.pojo.Row;
import com.shangdiao.sd_server.service.CompanyService;
import com.shangdiao.sd_server.service.DicService;
import com.shangdiao.sd_server.service.PostService;
import com.shangdiao.sd_server.service.RedisService;
import com.shangdiao.sd_server.util.CommonUtil;
import com.shangdiao.sd_server.util.DateUtil;
import com.shangdiao.sd_server.util.MD5Util;
import com.shangdiao.sd_server.util.RequestUtil;
import com.shangdiao.sd_server.util.StringUtil;

/**
 * 企业管理信息
 * @author mary
 *
 */
@Controller
@RequestMapping("/company/info")
public class PostInfoController {
	
	@Autowired
	DicService dicService;
	@Autowired
	CompanyService companyService;
	@Autowired
	RedisService redisService;
	@Autowired
	PostService postService;
	
	@RequestMapping(value = { "/getUnreadInfo.json" })
	@ResponseBody
	public ResponseMsg getUnreadInfo(HttpServletRequest request, HttpServletResponse response) {
		ResponseMsg retMsg = null;
		try{
			Integer companyId = RequestUtil.getInt(request, "uid");
			if(companyId == 0){
				retMsg = new ResponseMsg("缺少必要参数", ErrorCode.ERROR, null);
				return retMsg;
			}
			Row result = new Row();
			SdInternshipExample intExample = new SdInternshipExample();
			intExample.or().andCompanyIdEqualTo(companyId)
			.andStatusEqualTo(Constant.STATUS_ON)
			.andDelEqualTo(Constant.DEL_NO);
			List<SdInternship> intList = postService.selectInternshipList(intExample);
			int count = 0;
			if(intList.size() != 0){
				List<Integer> internshipIdList = new ArrayList<Integer>();
				for(SdInternship internship : intList){
					internshipIdList.add(internship.getInternshipId());
				}
				SdPInternshipExample pIntExample = new SdPInternshipExample();
				pIntExample.or().andInternshipIdIn(internshipIdList)
					.andSourceEqualTo(Constant.STATUS_ON)//个人
					.andStatusEqualTo(Constant.STATUS_ON);//未读
				count = postService.countPInternship(pIntExample);
			}
			result.put("unreadCount", count);
			return new ResponseMsg("查询成功", ErrorCode.OK, result);
		}catch(Exception e){
			e.printStackTrace();
			return new ResponseMsg("查询失败", ErrorCode.SERVER_ERROR, null);
		}
	}
	
	@RequestMapping(value = { "/updateInfo.json" })
	@ResponseBody
	@Transactional
	public ResponseMsg updateInfo(HttpServletRequest request, HttpServletResponse response) {
		ResponseMsg retMsg = null;
		try{
			Integer companyId = RequestUtil.getInt(request, "uid");
			String companyName = RequestUtil.get(request, "companyName");
			Integer dicFieldId = RequestUtil.getInt(request, "dicFieldId");
			String dicFieldName = RequestUtil.get(request, "dicFieldName");
			String website = RequestUtil.get(request, "website");
			Integer districtId = RequestUtil.getInt(request, "districtId");
			String district = RequestUtil.get(request, "district");
			String location = RequestUtil.get(request, "location");
			String email = RequestUtil.get(request, "email");
			String tel = RequestUtil.get(request, "tel");
			String intro = RequestUtil.get(request, "intro");
			String logo = RequestUtil.get(request, "logo");
			String companyPhotoUrl = RequestUtil.get(request, "companyPhotoUrl");
			if(companyId == 0){
				retMsg = new ResponseMsg("缺少必要参数", ErrorCode.ERROR, null);
				return retMsg;
			}
			if(!StringUtil.isBlank(email) && !StringUtil.isEmail(email)){
				retMsg = new ResponseMsg("邮箱格式错误", ErrorCode.ERROR, null);
				return retMsg;
			}
			if(!StringUtil.isBlank(tel) && !StringUtil.isMobile(tel)){
				retMsg = new ResponseMsg("手机号格式错误", ErrorCode.ERROR, null);
				return retMsg;
			}
			//移动logo
			if(!StringUtil.isBlank(logo)){
				logo = logo.substring(logo.lastIndexOf("/") + 1, logo.length());
				CommonUtil.Move(Constant.TEMP_FILE_PATH + logo, Constant.LOGO_FILE_PATH + logo);
			}
			//移动照片墙
			String[] photoUrlArr = companyPhotoUrl.split(",");
			int photoUrlLength = photoUrlArr.length;
			String[] srcFileArr = new String[photoUrlLength];
			String[] destPathArr = new String[photoUrlLength];
			for(int i = 0; i < photoUrlLength; i ++){
				String photoUrl = photoUrlArr[i];
				photoUrl = photoUrl.substring(photoUrl.lastIndexOf("/") + 1, photoUrl.length());
				srcFileArr[i] = Constant.TEMP_FILE_PATH + photoUrl;
				destPathArr[i] = Constant.PHOTO_FILE_PATH + photoUrl;
			}
			//移动文件
			boolean moveFlag = CommonUtil.MoveMore(srcFileArr, destPathArr);
			if(moveFlag){
				SdCompanyPhotoExample companyPhotoExample = new SdCompanyPhotoExample();
				companyPhotoExample.or().andCompanyIdEqualTo(companyId)
					.andDelEqualTo(Constant.DEL_NO);
				SdCompanyPhoto companyPhoto = new SdCompanyPhoto();
				companyPhoto.setDel(Constant.DEL_YES);
				companyService.updateCompanyPhoto(companyPhoto, companyPhotoExample);
				for(int i = 0; i < photoUrlLength; i ++){
					String photoUrl = photoUrlArr[i];
					photoUrl = photoUrl.substring(photoUrl.lastIndexOf("/") + 1, photoUrl.length());
					companyPhoto = new SdCompanyPhoto();
					companyPhoto.setCompanyId(companyId);
					companyPhoto.setCompanyPhotoUrl(Constant.PHOTO_FILE_URL + photoUrl);
					companyPhoto.setCreateTime(new Date());
					companyService.insertCompanyPhoto(companyPhoto);
				}
				
				SdCompany company = new SdCompany();
				company.setCompanyId(companyId);
				company.setCompanyName(companyName);
				company.setDicFieldId(dicFieldId);
				company.setDicFieldName(dicFieldName);
				company.setWebsite(website);
				company.setDistrictId(districtId);
				company.setDistrict(district);
				company.setLocation(location);
				company.setEmail(email);
				company.setTel(tel);
				company.setIntro(intro);
				if(!StringUtil.isBlank(logo)){
					company.setLogo(Constant.LOGO_FILE_URL + logo);
				}
				companyService.updateCompanyById(company);
				return new ResponseMsg("更新成功", ErrorCode.OK, null);
			}
			return new ResponseMsg("移动文件失败", ErrorCode.ERROR, null);
		}catch(Exception e){
			e.printStackTrace();
			return new ResponseMsg("更新失败", ErrorCode.SERVER_ERROR, null);
		}
	}
	
	
	@RequestMapping(value = { "/newInternship.json" })
	@ResponseBody
	public ResponseMsg newInternship(HttpServletRequest request, HttpServletResponse response) {
		ResponseMsg retMsg = null;
		try{
			Integer companyId = RequestUtil.getInt(request, "uid");
			Integer internshipId = RequestUtil.getInt(request, "internshipId", 0);
			Integer dicInternshipFieldId = RequestUtil.getInt(request, "dicInternshipFieldId", Constant.DEFAULT_INTEGER);
			String dicInternshipFieldName = RequestUtil.get(request, "dicInternshipFieldName");
			String internshipName = RequestUtil.get(request, "internshipName");
			String minSalary = RequestUtil.get(request, "minSalary");
			String maxSalary = RequestUtil.get(request, "maxSalary");
			Integer minEducationId = RequestUtil.getInt(request, "minEducationId", Constant.DEFAULT_INTEGER);
			String minEducationName = RequestUtil.get(request, "minEducationName");
			Integer recruitingNumbers = RequestUtil.getInt(request, "recruitingNumbers", Constant.DEFAULT_INTEGER);
			Integer districtId = RequestUtil.getInt(request, "districtId", Constant.DEFAULT_INTEGER);
			String district = RequestUtil.get(request, "district");
			String location = RequestUtil.get(request, "location");
			String description = RequestUtil.get(request, "desc");
			String requirement = RequestUtil.get(request, "requirement");
			String tel = RequestUtil.get(request, "tel");
			if(companyId == 0 || StringUtil.isBlank(internshipName)){
				retMsg = new ResponseMsg("缺少必要参数", ErrorCode.ERROR, null);
				return retMsg;
			}
			if((!StringUtil.isBlank(maxSalary) && !StringUtil.isNumber(maxSalary)) || (!StringUtil.isBlank(minSalary) && !StringUtil.isNumber(minSalary))){
				retMsg = new ResponseMsg("参数格式错误", ErrorCode.ERROR, null);
				return retMsg;
			}
			SdInternship internship = new SdInternship();//默认新增
			if(internshipId != 0){//编辑
				internship = postService.selectInternship(internshipId);
			}
			internship.setCompanyId(companyId);
			if(dicInternshipFieldId != Constant.DEFAULT_INTEGER){
				internship.setDicInternshipFieldId(dicInternshipFieldId);
			}
			if(!StringUtil.isBlank(dicInternshipFieldName)){
				internship.setDicInternshipFieldName(dicInternshipFieldName);
			}
			if(!StringUtil.isBlank(internshipName)){
				internship.setInternshipName(internshipName);
			}
			if(!StringUtil.isBlank(minSalary)){
				internship.setMinSalary(Double.valueOf(minSalary));
			}
			if(!StringUtil.isBlank(maxSalary)){
				internship.setMaxSalary(Double.valueOf(maxSalary));
			}
			if(minEducationId != Constant.DEFAULT_INTEGER){
				internship.setMinEducationId(minEducationId);
			}
			if(!StringUtil.isBlank(minEducationName)){
				internship.setMinEducationName(minEducationName);
			}
			if(recruitingNumbers != Constant.DEFAULT_INTEGER){
				internship.setRecruitingNumbers(recruitingNumbers);
			}
			if(districtId != Constant.DEFAULT_INTEGER){
				internship.setDistrictId(districtId);
			}
			if(!StringUtil.isBlank(district)){
				internship.setDistrict(district);
			}
			if(!StringUtil.isBlank(location)){
				internship.setLocation(location);
			}
			if(!StringUtil.isBlank(description)){
				internship.setDescription(description);
			}
			if(!StringUtil.isBlank(requirement)){
				internship.setRequirement(requirement);
			}
			if(!StringUtil.isBlank(tel)){
				internship.setTel(tel);
			}
			if(internshipId != 0){//编辑
				postService.updateInternship(internship);
				return new ResponseMsg("更新实习岗位成功", ErrorCode.OK, null);
			}else{
				internship.setCreateDate(new Date());
				internship.setStatus(Constant.STATUS_ON);
				postService.insertInternship(internship);
				return new ResponseMsg("新增实习岗位成功", ErrorCode.OK, null);
			}
		}catch(Exception e){
			e.printStackTrace();
			return new ResponseMsg("新增实习岗位失败", ErrorCode.SERVER_ERROR, null);
		}
	}
	
	
	@RequestMapping(value = { "/newParttime.json" })
	@ResponseBody
	public ResponseMsg newParttime(HttpServletRequest request, HttpServletResponse response) {
		ResponseMsg retMsg = null;
		try{
			Integer companyId = RequestUtil.getInt(request, "uid");
			Integer parttimeId = RequestUtil.getInt(request, "parttimeId", 0);
			Integer dicParttimeFieldId = RequestUtil.getInt(request, "dicParttimeFieldId", Constant.DEFAULT_INTEGER);
			String dicParttimeFieldName = RequestUtil.get(request, "dicParttimeFieldName");
			Integer dicSettlementId = RequestUtil.getInt(request, "dicSettlementId", Constant.DEFAULT_INTEGER);
			String dicSettlementName = RequestUtil.get(request, "dicSettlementName");
			String salary = RequestUtil.get(request, "salary");
			String unit = RequestUtil.get(request, "unit");
			Integer hasAllowance = RequestUtil.getInt(request, "hasAllowance");
			String startData = RequestUtil.get(request, "startData");
			String endData = RequestUtil.get(request, "endData");
			String startTime = RequestUtil.get(request, "startTime");
			String endTime = RequestUtil.get(request, "endTime");
			Integer recruitingNumbers = RequestUtil.getInt(request, "recruitingNumbers", Constant.DEFAULT_INTEGER);
			Integer districtId = RequestUtil.getInt(request, "districtId", Constant.DEFAULT_INTEGER);
			String district = RequestUtil.get(request, "district");
			String location = RequestUtil.get(request, "location");
			String description = RequestUtil.get(request, "desc");
			String requirement = RequestUtil.get(request, "requirement");
			String deadline = RequestUtil.get(request, "deadline");
			String tel = RequestUtil.get(request, "tel");
			if(companyId == 0 ){
				retMsg = new ResponseMsg("缺少必要参数", ErrorCode.ERROR, null);
				return retMsg;
			}
			SdParttime parttime = new SdParttime();//默认新增
			if(parttimeId != 0){
				parttime = postService.selectParttime(parttimeId);
			}
			parttime.setCompanyId(companyId);
			if(dicParttimeFieldId != Constant.DEFAULT_INTEGER){
				parttime.setDicParttimeFieldId(dicParttimeFieldId);
			}
			if(!StringUtil.isBlank(dicParttimeFieldName)){
				parttime.setDicParttimeFieldName(dicParttimeFieldName);
			}
			if(dicSettlementId != Constant.DEFAULT_INTEGER){
				parttime.setDicSettlementId(dicSettlementId);
			}
			if(!StringUtil.isBlank(dicSettlementName)){
				parttime.setDicSettlementName(dicSettlementName);
			}
			if(!StringUtil.isBlank(salary)){
				parttime.setSalary(salary);
			}
			if(!StringUtil.isBlank(unit)){
				parttime.setUnit(unit);
			}
			if(hasAllowance != Constant.DEFAULT_INTEGER){
				parttime.setHasAllowance(hasAllowance);
			}
			if(!StringUtil.isBlank(startData)){
				parttime.setStartData(DateUtil.StringtoDate(startData, DateUtil.LONG_DATE_FORMAT));
			}
			if(!StringUtil.isBlank(endData)){
				parttime.setEndData(DateUtil.StringtoDate(endData, DateUtil.LONG_DATE_FORMAT));
			}
			if(!StringUtil.isBlank(startTime)){
				parttime.setStartTime(DateUtil.StringtoDate(startData + " " + startTime, DateUtil.FORMAT_TWO));
			}
			if(!StringUtil.isBlank(endTime)){
				parttime.setEndTime(DateUtil.StringtoDate(endData + " " + endTime, DateUtil.FORMAT_TWO));
			}
			if(recruitingNumbers != Constant.DEFAULT_INTEGER){
				parttime.setRecruitingNumbers(recruitingNumbers);
			}
			if(districtId != Constant.DEFAULT_INTEGER){
				parttime.setDistrictId(districtId);
			}
			if(!StringUtil.isBlank(district)){
				parttime.setDistrict(district);
			}
			if(!StringUtil.isBlank(location)){
				parttime.setLocation(location);
			}
			if(!StringUtil.isBlank(description)){
				parttime.setDescription(description);
			}
			if(!StringUtil.isBlank(requirement)){
				parttime.setRequirement(requirement);
			}
			if(!StringUtil.isBlank(deadline)){
				Date deadlineDate = DateUtil.StringtoDate(deadline, DateUtil.LONG_DATE_FORMAT);
				if(deadlineDate.before(new Date())){
					return new ResponseMsg("截止日期设置过近", ErrorCode.ERROR, null);
				}
				parttime.setDeadline(deadlineDate);
			}
			if(!StringUtil.isBlank(tel)){
				parttime.setTel(tel);
			}
			if(parttimeId != 0){//更新
				postService.updateParttime(parttime);
				return new ResponseMsg("更新兼职岗位成功", ErrorCode.OK, null);
			}else{//新增
				parttime.setCreateTime(new Date());
				parttime.setStatus(Constant.STATUS_ON);
				postService.insertParttime(parttime);
				return new ResponseMsg("新增兼职岗位成功", ErrorCode.OK, null);
			}
		}catch(Exception e){
			e.printStackTrace();
			return new ResponseMsg("新增兼职岗位失败", ErrorCode.SERVER_ERROR, null);
		}
	}
	
	/**
	 * 删除实习岗位
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = { "/deleteInternship.json"})
	@ResponseBody
	public ResponseMsg deleteInternship(HttpServletRequest request, HttpServletResponse response) {
		try{
			Integer internshipId = RequestUtil.getInt(request, "internshipId");
			SdInternship internship = new SdInternship();
			internship.setInternshipId(internshipId);
			internship.setDel(Constant.DEL_YES);
			int count = postService.updateInternship(internship);
			if(count == 1){
				return new ResponseMsg("删除实习岗位成功", ErrorCode.OK, null);
			}else{
				return new ResponseMsg("删除实习岗位失败", ErrorCode.ERROR, null);
			}
		}catch(Exception e){
			e.printStackTrace();
			return new ResponseMsg("删除实习岗位失败", ErrorCode.SERVER_ERROR, null);
		}
	}
	
	/**
	 * 删除兼职岗位
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = { "/deleteParttime.json" })
	@ResponseBody
	@Transactional
	public ResponseMsg deleteParttime(HttpServletRequest request, HttpServletResponse response) {
		try{
			Integer parttimeId = RequestUtil.getInt(request, "parttimeId");
			SdParttime parttime = new SdParttime();
			parttime.setParttimeId(parttimeId);
			parttime.setDel(Constant.DEL_YES);
			int count = postService.updateParttime(parttime);
			if(count == 1){
				return new ResponseMsg("删除兼职岗位成功", ErrorCode.OK, null);
			}else{
				return new ResponseMsg("删除兼职岗位失败", ErrorCode.ERROR, null);
			}
		}catch(Exception e){
			e.printStackTrace();
			return new ResponseMsg("删除兼职岗位失败", ErrorCode.SERVER_ERROR, null);
		}
	}
	
	@RequestMapping(value = { "/resetPwd.json" })
	@ResponseBody
	public ResponseMsg resetPwde(HttpServletRequest request, HttpServletResponse response) {
		try{
			String oldPwd = RequestUtil.get(request, "oldPwd");
			String newPwd = RequestUtil.get(request, "newPwd");
			Integer companyId = RequestUtil.getInt(request, "uid");
			if(StringUtil.isBlank(oldPwd) || StringUtil.isBlank(newPwd)){
				return new ResponseMsg("缺少必要参数", ErrorCode.ERROR, null);
			}
			SdCompanyBase companyBase = companyService.selectCompanyBaseById(companyId);
			if(companyBase.getPwd().equals(MD5Util.encodeByMD5(oldPwd))){
				companyBase.setPwd(MD5Util.encodeByMD5(newPwd));
				companyService.updateCompanyBaseById(companyBase);
				return new ResponseMsg("修改密码成功", ErrorCode.OK, null);
			}else{
				return new ResponseMsg("原密码错误", ErrorCode.ERROR, null);
			}
		}catch(Exception e){
			e.printStackTrace();
			return new ResponseMsg("修改密码失败", ErrorCode.SERVER_ERROR, null);
		}
	}
	
	@RequestMapping(value = { "/feedback.json" })
	@ResponseBody
	public ResponseMsg feedback(HttpServletRequest request, HttpServletResponse response) {
		try{
			Integer companyId = RequestUtil.getInt(request, "uid");
			String content = RequestUtil.get(request, "content");
			if(companyId == 0 ){
				return new ResponseMsg("缺少必要参数", ErrorCode.ERROR, null);
			}
			SdFeedback feedback = new SdFeedback();
			feedback.setContent(content);
			feedback.setUserId(companyId);
			feedback.setEntryFlag("c");
			feedback.setCreateTime(new Date());
			feedback.setDel(Constant.DEL_NO);
			companyService.insertFeedback(feedback);
			return new ResponseMsg("反馈成功", ErrorCode.OK, null);
		}catch(Exception e){
			e.printStackTrace();
			return new ResponseMsg("反馈失败", ErrorCode.SERVER_ERROR, null);
		}
	}
	
	public static void main(String[] args) {
		
	}
	
}
